package es.udc.emarketapp.http.controller.actions.categories;

import es.udc.emarketapp.http.controller.actions.products.*;
import es.udc.emarketapp.emarket.model.category.vo.CategoryVO;
import es.udc.emarketapp.emarket.model.categoryFacade.CategoryFacade;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class FWDCreateCategoryAction extends DefaultAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";

    @Override
    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        ActionMessages errors = new ActionMessages();

        try {

            CategoryFacade categoryFacade = new CategoryFacade();

            HttpSession session = request.getSession(true);
            Long idCat = (Long) request.getAttribute("idCat");

            request.setAttribute("idCat", idCat);
            /*path de categoria actual seleccionada*/
            List<CategoryVO> listCats = categoryFacade.getCategoryRoute(idCat);
            session.setAttribute("currentCategories", listCats);

        } catch (InstanceNotFoundException e) {
            errors.add("CategoryIdentifier",
                    new ActionMessage("ErrorMessages.category.notFound"));
            saveErrors(request, errors);

            return new ActionForward(mapping.getInput());
        }

        return mapping.findForward("UpdateProductDetailsForm");
    }
}
